﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Budget.Silver.OutcomeServiceReference;
using Budget.Silver.Model.Calendar;
using GalaSoft.MvvmLight;

namespace Budget.Silver.ViewModel
{
    public class CalendarViewModel : ViewModelBase
    {
        private int year, month;
        private MonthModel monthModel;
        private DayModel selectedDay;

        private void RaiseSelectDay(CalendarEventArgs e)
        {
            if (SelectDay != null) SelectDay(this, e);
        }

        public event EventHandler<CalendarEventArgs> SelectDay;

        public int Year
        {
            get { return year; }
            set
            {
                if (year != value)
                {
                    year = value;
                    RaisePropertyChanged(() => Year);
                }
            }
        }

        public int Month 
        {
            get { return month; }
            set
            {
                if (month != value)
                {
                    month = value;
                    RaisePropertyChanged(() => Month);
                }
            }
        }

        public ICollection<string> Weeks
        {
            get { return new Collection<string> {"Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"}; }
        }

        public MonthModel MonthModel
        {
            get { return monthModel; }
            set
            {
                if (!Equals(monthModel, value))
                {
                    monthModel = value;

                    RaisePropertyChanged(() => MonthModel);
                }
            }
        }

        public DayModel SelectedDay
        {
            get { return selectedDay; }
            set
            {
                if(!Equals(selectedDay, value))
                {
                    selectedDay = value;
                    RaisePropertyChanged(() => SelectedDay);
                    RaiseSelectDay(new CalendarEventArgs { SelectedDay = selectedDay });
                }
            }
        }

        public void SelectMonth(ObservableCollection<OutcomeOrderData> orders, int y, int m)
        {
            Year = y;
            Month = m;
            MonthModel = new MonthModel(orders, y, m);
        }

        public void SelectMonthAndDate(ObservableCollection<OutcomeOrderData> orders, DateTime date)
        {
            SelectMonth(orders, date.Year, date.Month);
            SelectDate(date);
        }

        public void SelectDate(DateTime date)
        {
            SelectedDay = MonthModel.Days.First(d => d.Date.Date == date.Date);
        }
    }
}